﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using ContractManagermentSystem.Business;
using ContractManagermentSystem.Entity;

namespace ContractManagermentSystem.Pages
{
    public partial class RoleManagement : System.Web.UI.Page
    {
        private RoleBus _roleBus = new RoleBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAllRole();
            }
        }

        private void LoadAllRole()
        {
            RoleGridView.DataSource = _roleBus.LoadAllRole();
            RoleGridView.DataBind();
        }

        protected void RoleGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            var role = new Role();
            Guid roleId = (Guid)RoleGridView.DataKeys[RoleGridView.SelectedIndex].Value;
            role.RoleId = roleId;
            DataTable dataTable = _roleBus.GetRoleById(roleId);
            bool status = bool.Parse(dataTable.Rows[0]["Status"].ToString());
            if (status == true)
            {
                role.Status = false;
                _roleBus.ChangeStatusRole(role);
            }
            else
            {
                role.Status = true;
                _roleBus.ChangeStatusRole(role);
            }
            LoadAllRole();
        }

        protected void RoleGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var role = new Role();
            Guid roleId = new Guid(RoleGridView.DataKeys[e.RowIndex].Values[0].ToString());
            role.RoleId = roleId;
            _roleBus.DeleteRoleById(role);
            LoadAllRole();
        }

        protected void RoleGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            RoleGridView.PageIndex = e.NewPageIndex;
            LoadAllRole();
        }
    }
}
